using System;
using System.Collections;
using Anvil.Core;
using Anvil.Core.Commands;

namespace Anvil.Extras.Commands
{
	/// <summary>
	/// Create dynamic KeyValue list of dates relative to "now".
	/// </summary>
	public class RelativeDateKeyList : BaseLogic
	{

        public const string TODAY = "Today";
        public const string THIS_WEEK = "This Week";
        public const string NEXT_WEEK = "Next Week";
        public const string THIS_MONTH = "This Month";
        public const string NEXT_MONTH = "Next Month";

		private void AddDate(IList list, string label, DateTime anchor, double offset)
		{
			DateTime newDate = anchor.AddDays(offset);
			KeyValue entry = new KeyValue(newDate.ToShortDateString(), label);
			list.Add(entry);
		}

		/// <summary>
		/// Generate the relative date list based on StartDate.
		/// </summary>
		/// <param name="startDate">The anchor date</param>
		/// <returns>A List of KeyValue objects for the standard date entries</returns>
		public IList DateList(DateTime startDate)
		{
			KeyValueList list = new KeyValueList();

			// Today
			DateTime Today = startDate;
			double days = 0;
            AddDate(list, TODAY, Today, days);

			// This Week 
			days = DayOfWeek.Saturday - Today.DayOfWeek;
            AddDate(list, THIS_WEEK, Today, days);

			// Next Week 
			days = 7 + (DayOfWeek.Saturday - Today.DayOfWeek);
			AddDate(list, NEXT_WEEK, Today, days);

			// This Month
			days = (0 - Today.Day) + 1;
			DateTime thisAlpha = Today.AddDays(days);
			DateTime thisMonth = thisAlpha.AddMonths(1).AddDays(-1);
			AddDate(list, THIS_MONTH, thisMonth, 0);

			// Next Month
			DateTime nextMonth = thisAlpha.AddMonths(2).AddDays(-1);
			AddDate(list, NEXT_MONTH, nextMonth, 0);

			return list;
		}

		public override bool RequestExecute(IRequestContext context)
		{
			IList list = DateList(DateTime.Today);
			context.Add(ID, list);
			return CONTINUE;
		}
	}
}